<?php
namespace app\admin\service\game;


use app\admin\model\game\BoxPcGameSource;
use app\admin\model\User;
use app\admin\service\BaseService;
use think\Db;
use think\db\exception\BindParamException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\exception\PDOException;

class BoxPcGameSourceService extends BaseService
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new BoxPcGameSource();
    }

    /**
     * Desc : getList
     * User : Steven
     * Date : 2024-07-16 11:13
     * @return array
     */
    public function getList(): array
    {
        $param = request()->param();
        // 查询条件
        $map = [];
        // 游戏
        if (!empty($param['package_id'])) {
            $map[] = ['package_id', '=', $param['package_id']];
        }
        if (!empty($param['engine_key'])) {
            $map[] = ['engine_key', '=', $param['engine_key']];
        }
        $map[] = ['mark', '=', 1];
        $list = $this->model->where($map)->order('id desc')->paginate($param['limit'] ?? 10)->toArray();
        return [
            "msg" => '操作成功',
            "code" => 0,
            "data" => $list['data'] ?? [],
            "count" => $list['total'] ?? 0,
        ];
    }

    /**
     * Desc : edit
     * User : Steven
     * Date : 2024-07-16 13:09
     * @return array
     * @throws BindParamException
     * @throws PDOException
     */
    public function edit(): array
    {
        // 获取参数
        $argList = func_get_args();
        // 查询条件
        $data = $argList[0] ?? [];
        // 是否打印SQL
        $is_sql = $argList[1] ?? false;
        if (!$data) {
            $data = request()->param();
        }
        $error = '';
        if(!isset($data['id'])){
            $info = $this->model->where('package_id','=',$data['package_id'])->find();
            if($info){
                return message('原包已存在', false);
            }
        }
        $userId = request()->getUserId();
        $data['op_admin_id'] = $userId;
        $data['op_admin_account'] = (new User())->where('id','=',$userId)->value('nickname');
        $rowId = $this->model->edit($data, $error, $is_sql);
        if(!$rowId) return message($error, false);
        return message('操作成功', true, $rowId);
    }

}